﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace EntityDA
{
    public class LoaiSachDB
    {
        //lấy ra danh sách các loại sách
        public List<LoaiSachData> GetAllLoaiSach()
        {
            List<LoaiSachData> DSLoaiSach = new List<LoaiSachData>();
            ConnectionDB cn = new ConnectionDB();
            string sql = "select * from LoaiSach";
            cn.cmd.CommandText = sql;
            SqlDataReader reader = cn.cmd.ExecuteReader();
            LoaiSachData LoaiSach = null;
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    LoaiSach = new LoaiSachData();
                    if (!reader.IsDBNull(0))
                        LoaiSach.MaLoai = reader.GetInt32(0);
                    if (!reader.IsDBNull(1))
                        LoaiSach.TenLoai = reader.GetString(1);
                    if (!reader.IsDBNull(2))
                        LoaiSach.HinhLoai = reader.GetString(2);
                    DSLoaiSach.Add(LoaiSach);
                }
            }
            return DSLoaiSach;
        }

        //lấy ra 1 loại sách
        public LoaiSachData GetOneLoaiSach(int MaLoai)
        {
            ConnectionDB cn = new ConnectionDB();
            string sql = "";
            if (MaLoai == -1)
                sql = "select * from LoaiSach";
            else
            {
                sql = "select * from LoaiSach where MaLoai=@MaLoai";
                cn.cmd.Parameters.Add(new SqlParameter("@MaLoai", MaLoai));
            }
            cn.cmd.CommandText = sql;
            SqlDataReader reader = cn.cmd.ExecuteReader();
            LoaiSachData LoaiSach = null;
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    LoaiSach = new LoaiSachData();
                    if (!reader.IsDBNull(0))
                        LoaiSach.MaLoai = reader.GetInt32(0);
                    if (!reader.IsDBNull(1))
                        LoaiSach.TenLoai = reader.GetString(1);
                    if (!reader.IsDBNull(2))
                        try
                        {
                            LoaiSach.HinhLoai = reader.GetString(2);
                        }
                        catch
                        {
                        }
                }
            }
            return LoaiSach;
        }

    }
}